//
//  FileHelper.m
//  iEPD
//
//  Created by xavi on 1/9/13.
//  Copyright (c) 2013 Mobioneer Ltd. All rights reserved.
//

#import "FileHelper.h"

@implementation FileHelper
- (id)initWithPath:(NSString *)path
{
    self.fileManager = [NSFileManager defaultManager];
    self.paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    self.documentsDir = [self.paths objectAtIndex:0];
    
    self.currentPath = [self.fileManager currentDirectoryPath];
    NSString *mediaDir = [self.documentsDir stringByAppendingPathComponent:@"Media"];
    NSString *photoDir = [self.documentsDir stringByAppendingPathComponent:@"Media/Photos"];
    NSString *videoDir = [self.documentsDir stringByAppendingPathComponent:@"Media/Videos"];
    if (![self.fileManager fileExistsAtPath:mediaDir]) {
        [self.fileManager createDirectoryAtPath:mediaDir withIntermediateDirectories:YES attributes:nil error:NULL];
        
    }
    if (![self.fileManager fileExistsAtPath:photoDir]) {
        [self.fileManager createDirectoryAtPath:photoDir withIntermediateDirectories:YES attributes:nil error:NULL];
    } else{
        NSLog(@"Directory does exist");
    }
    if (![self.fileManager fileExistsAtPath:videoDir]) {
        [self.fileManager createDirectoryAtPath:videoDir withIntermediateDirectories:YES attributes:nil error:NULL];
    } else{
        NSLog(@"Dicrectory does exist");
    }
    [self.documentsDir stringByAppendingPathComponent:@"Media"];
    self.documentsDir = [self.documentsDir stringByAppendingPathComponent:path];
    return self;
}
-(NSString *)getType
{
    NSDictionary *dic = [self.fileManager attributesOfFileSystemForPath:self.documentsDir error:NULL];
    return [dic objectForKey:NSFileType];
}

- (NSArray *)listDir
{
    NSLog(@"self.docDir :%@",self.documentsDir);
    NSArray *array =[self.fileManager contentsOfDirectoryAtPath:self.documentsDir error:NULL];
    
    for (int i = 0; i<  [array count]; i++) {
        NSLog(@"name : %@",array[i]);
    }
    return [self.fileManager contentsOfDirectoryAtPath:self.documentsDir error:NULL];
}

- (NSString *)getCurrentPath
{
    NSLog(@"Current path : %@",self.currentPath);
    return self.currentPath;
}
- (NSString *)getPath:(NSString *)path
{
    [self.currentPath stringByAppendingPathComponent:path];
    return self.currentPath;
}
- (NSDictionary *)getAttributes:(NSString *)path
{
    NSDictionary *dic = [self.fileManager attributesOfItemAtPath:path error:nil];
    return  dic;
 
}
- (void)changeCurrentPath:(NSString *)path
{
    if ([self.fileManager changeCurrentDirectoryPath:path]) {
        NSLog(@"change successfully");
    }
    else{
        NSLog(@"file does not exist");
    }
}
- (void)createDir:(NSString *)path
{
    [self.documentsDir stringByAppendingPathComponent:path];
    if (![self.fileManager fileExistsAtPath:self.documentsDir]) {
        [self.fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:NULL];

    }
   }
- (void)deleteDir
{
    
}
- (void)renameDir
{
    
}
- (void)cutDir
{
    
}
@end
